<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Uniboard Twitter Widget</title>
    <link href="index.css" media="all" rel="stylesheet" type="text/css"/>
    <link href="jquery.tweet.css" media="all" rel="stylesheet" type="text/css"/>
    <link href="jquery.tweet.query.css" media="all" rel="stylesheet" type="text/css"/>
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
    <script language="javascript" src="js/jquery.tweet.js" type="text/javascript"></script>
    <script type='text/javascript'>
		
	  var un;
	  var hidden = $('<div id="hid"></div>');
		  hidden.hide();			

      jQuery(document).ready(function($) {
			$('body').append(hidden);
			hidden.hide();
			
			if(window.uniboard){

				if(window.uniboard.preferenceKeys().length != 0){

					var searchInput = window.uniboard.preference("searchInput");
					var username = window.uniboard.preference("user");
					
					if(searchInput === "" && username !=""){
						
						loginTweeter(username);
						resizer(215,450);
						
					}else if (username === "" && searchInput !=""){
						
							searchTweets(searchInput);
							resizer(215,450);
					}
				}else{
					
						modeSelector();
					
				}
			};	
			
 		});
	function resizer(w,h){
			if(window.uniboard){
				window.uniboard.resize(w,h);	
			}
	}	
	 function searchTweets (input) {
	
		var tweeter = $("<div id='query'></div>");		
			$("#container").empty();
			$("#container").append(tweeter);
			   
		tweeter.tweet({
			          avatar_size: 48,
			          count: 14,
			          query: input,
			          loading_text: "searching twitter..."
			        });
			if(window.uniboard){
				window.uniboard.setPreference("searchInput", input);
			}	
	    };
	
	function loginTweeter(um){
		var tweeter = $("<div id='query'></div>");
				$("#container").empty();
				$("#container").append(tweeter);
						
		tweeter.tweet({
			username: um,
	          avatar_size: 48,
	          count: 14,
	          loading_text: "searching twitter..."
	        });
			
		if(window.uniboard){
			window.uniboard.setPreference("user", um);
		}
	};
	
	function modeSelector(){
		$("#container").empty();
		var myAccount = $('<div><img src="./images/user.png"><a>Follow</a></div>')
			.addClass("button")
			.click(function(){
				resizer(215,193);
				userInfo();
			});
		var search = $('<div><img src="./images/search.png"><a>Search</a></div>')
			.addClass("button")
			.click(function(){
				resizer(215,193);
				searchInfo();
			});
		var modePanel = $('<div></div>')
			.addClass('panel')
			.append(myAccount)
			.append(search);
		$("#container").append(modePanel);	
	}
	
	function userInfo(){
		$("#container").find('.userPanel').remove();
		var textField = $("<div>Who do you want to follow?<form onsubmit='$(\"#submit\").trigger(\"click\")' method='get'><input type='text' id='user'></input></form></div>")
			.addClass('userPanel');
		
		var submit = $("<div id='submit'>Follow</div>")
			.addClass('submitbut')
			.click(function(){
				un = $('#user').val();
				loginTweeter(un);
				resizer(215,450);
				
			});
		textField.append(submit);
		
		$(".panel").append(textField);
		setTimeout('refreshUser()',10000);
		
		
	}
	
	
	function searchInfo(){
		$("#container").find('.userPanel').remove();
		var textField = $("<div>Search:<form onsubmit='$(\"#submit\").trigger(\"click\")' method='get'><input type='text' id='user'></input></form></div>")
			.addClass('userPanel');
		
		var submit = $("<div id='submit'>Search</div>")
			.addClass('submitbut')
			.click(function(){
				un = $('#user').val();
				searchTweets(un);
				resizer(215,450);
			});
		textField.append(submit);
		
		$(".panel").append(textField);
		setTimeout('refreshSearch()',10000);
	}
	
	 function refreshUser(){
		
		$('#hid').empty();	
		$('#hid').tweet 
		({
		 username: un,
         avatar_size: 48,
         count: 14,
         loading_text: "pass"
		});
		setTimeout('verifyLastTweet()',5000);
		setTimeout('refreshUser()',10000);
	} 
	 function refreshSearch(){
		$('#hid').empty();	
		$('#hid').tweet 
		({
		 query: un,
         avatar_size: 48,
         count: 14,
         loading_text: "pass"
		});
		setTimeout('verifyLastTweet()',5000);
		setTimeout('refreshSearch()',10000);
	}   
	   function verifyLastTweet(){
		if($('#hid').find('.tweet_first').length >0 === false){
			return false;
		}
		
		if($('#hid').find('.tweet_first').text() === $('#container').find('.tweet_first').text()){
			return false;
		}
		
		if($('#hid').find('.tweet_first').text() === "pass"){
			return false;
		}
		
		if($('#hid').find('.tweet_first').text() != $('#container').find('.tweet_first').text() && $('#hid').find('.tweet_first').text() != "pass"){
				if($('#query').find('.tweet_list').text() != ''){
					$('#query').find('.tweet_list').remove();
					$('#hid').find('.tweet_list').clone().prependTo($('#query'));
				
			}
		
		}
	
		
	} 
    </script>
  </head>
  <body>
    <div id='container'>

      
      
    </div>
  </body>
</html>
